#include "stdafx.h"

using namespace rapidxml;

LevelMgr::LevelMgr()
{
	//**load comment string from a file
	//**added later
	commentStringMap[1] = L"street battle\ntarget:kill all enemies";
}

LevelMgr::~LevelMgr()
{
}

LevelMgr * LevelMgr::getInstance()
{
	static LevelMgr manager;

	return &manager;
}

std::wstring LevelMgr::getCommentString(int num)
{
	//return commentStringMap[num];

	return L"street battle\ntarget:kill all enemies";
}

void LevelMgr::loadLevel(const std::wstring & levelname)
{
	Game::getInstance()->newGame();

	xml_document<wchar_t> doc;
	std::wifstream file(levelname);
	wchar_t str[6000] = { 0 };
	int i = 0;
	while (!file.eof())
	{
		file.get(str[i++]);
	}

	doc.parse<0>(str);
	xml_node<wchar_t> * level = doc.first_node();					//<level>

	xml_node<wchar_t> * ele = level->first_node(L"timelimit");		//<timelimit>
	int timelimit = _wtoi(ele->value());							//timelimit
	Game::getInstance()->timelimit = timelimit;

	ele = level->first_node(L"win");
	std::wstring win = ele->value();								//win
	if (win == L"KillAll")
	{

	}
	else if (win == L"EndPoint")
	{
		ele = level->first_node(L"endpoint");
		int x = _wtoi(ele->first_node(L"x")->value()) + LEFTMAP;
		int y = _wtoi(ele->first_node(L"y")->value()) + TOPMAP;
		std::wstring picname = ele->first_node(L"picname")->value();

		Game::getInstance()->endpoint = new EndPoint(picname, x, y);
	}
	else if (win == L"Collection")
	{
		ele = level->first_node(L"collection");
		int count = _wtoi(ele->first_node(L"count")->value());
		xml_node<wchar_t> * point = ele->first_node(L"pt");
		for (int i = 0; i < count; i++)
		{
			int x = _wtoi(point->first_node(L"x")->value()) + LEFTMAP;
			int y = _wtoi(point->first_node(L"y")->value()) + TOPMAP;
			std::wstring picname = point->first_node(L"picname")->value();

			CollectionMgr::getInstance()->create(picname, x, y);

			point = point->next_sibling();
		}
	}
	Game::getInstance()->setStrategy(GameStrategyMgr::getInstance()->get(win));

	
	ele = level->first_node(L"map");								//<map>
	Map::getInstance()->set(ele);									//set map

	ele = level->first_node(L"enemy");								//<enemy>
	xml_node<wchar_t> * enemy = ele->first_node(L"count");			//<count>
	int count = _wtoi(enemy->value());								//enemy count
	enemy = enemy->next_sibling();									//<e>
	for (int i = 0; i < count; i++)
	{
		xml_node<wchar_t> * bullet = enemy->first_node();			//<bullet>
		tagEnemy einfo;

		einfo.bname = bullet->first_node(L"name")->value();								//bullet name
		einfo.bulletInfo.v = _wtoi(bullet->first_node(L"velocity")->value());			//velocity
		einfo.bulletInfo.sname = bullet->first_node(L"sname")->value();					//strategy
		einfo.bulletInfo.lifespan = _wtoi(bullet->first_node(L"lifespan")->value());	//lifespan
		einfo.bulletInfo.picname = bullet->first_node(L"picname")->value();				//picname
		einfo.bulletInfo.rotatespeed = _wtoi(bullet->first_node(L"rot")->value());		//rotation speed
		einfo.bulletInfo.damage[0] = _wtoi(bullet->first_node(L"d1")->value());			//damage[0]
		einfo.bulletInfo.damage[1] = _wtoi(bullet->first_node(L"d2")->value());			//damage[1]
		einfo.bulletInfo.damage[2] = _wtoi(bullet->first_node(L"d3")->value());			//damage[2]
		einfo.bulletInfo.damage[3] = _wtoi(bullet->first_node(L"d4")->value());			//damage[3]
		
		int x = _wtoi(enemy->first_node(L"x")->value()) + LEFTMAP;						//x position reletive to world
		int y = _wtoi(enemy->first_node(L"y")->value()) + TOPMAP;						//y position reletive to world
		einfo.picname = enemy->first_node(L"picname")->value();							//picture of the enemy
		einfo.life = _wtoi(enemy->first_node(L"life")->value());						//life of the enemy
		einfo.v = _wtoi(enemy->first_node(L"v")->value());								//velocity if the enemy
		einfo.direction = _wtoi(enemy->first_node(L"dir")->value());					//direction
		einfo.shootinterval = _wtoi(enemy->first_node(L"shootinter")->value());			//shoot interval
		einfo.sname = enemy->first_node(L"sname")->value();

		EnemyMgr::getInstance()->create(einfo, x, y);	//create an enemy

		enemy = enemy->next_sibling();
	}

	ele = level->first_node(L"hero");								//<hero>
	std::wstring picname = ele->first_node(L"picname")->value();
	int x = _wtoi(ele->first_node(L"x")->value()) + LEFTMAP;
	int y = _wtoi(ele->first_node(L"y")->value()) + TOPMAP;
	int life = _wtoi(ele->first_node(L"life")->value());
	Game::getInstance()->newHero(new HeroTank(picname, x, y, DIR_UP, life));

	ele = level->first_node(L"property");
	if (ele)
	{
		PropertyMgr::getInstance()->create(ele);
	}
}

void LevelMgr::loadLevel(int num)
{
	wchar_t temp[50];

	_stprintf_s(temp, L"level\\level%d.xml", num);
	loadLevel(temp);
}